(function(){
    var heught_l=$(".lian_seach").height();
    $(".du_seach").css("height",heught_l);
    $.ajax({
        type: 'POST',
        url: 'twoCascades',
        dataType: 'JSON',
        crossDomian: true,
        data: {},
        success: function(data){
            var province  = data[0];
            for (var i  in province) {
                var str = "<option value='"+i+"'>"+province[i]+"</option>";
                $('.province').append(str);
            }
            var city = data[1];
            $('.province').change(function(){
                var index = this.selectedIndex;
                var children = $('.city').children();
                for (var i = 0; i < children.length; i++) {
                    children[i].remove();
                }
                var str = "<option value='0'>请选择城市</option>";
                $('.city').append(str);
                for (var i in city[index - 1]) {
                    var str = "<option value='"+i+"'>"+city[index - 1][i]+"</option>";
                    $('.city').append(str);
                }
            });
        }
    });
}());

window.onresize = function() {
    var heught_l = $(".lian_seach").height();
    $(".du_seach").css("height", heught_l);
};

var crrStatus = 0
$('.radio').click(function () {
    var value = $(this).val();
    crrStatus = value;
    switch(value) {
        case "0":
            $('.month_a').attr("disabled", "disabled");
            $('.month_b').attr("disabled", "disabled");
            $('.select_time').removeAttr("disabled");
            break;
        case "1":
            $('.select_time').attr("disabled", "disabled");
            $('.month_a').removeAttr("disabled");
            $('.month_b').removeAttr("disabled");
            break;
    }
});

var msg = '';
$('.btn_seach_success').click(function () {
    var status = $(this).val();
    var usernum = $(".gwusernum").val();
    if (status == 0 && usernum == '') {
        sweetAlert('', '岗位号不能为空', 'error');
        return;
    }
    msg = 'success';
    gwPersonnelList(status);
});

$('.btn_seach_overdue').click(function () {
    var status = $(this).val();
    var usernum = $(".gwusernum").val();
    if (status == 0 && usernum == '') {
        sweetAlert('', '岗位号不能为空', 'error');
        return;
    }
    msg = 'overdue';
    gwPersonnelList(status);
});

function gwPersonnelList(status)
{
    var usernum = $(".gwusernum").val();
    var month_a = $('.month_a').val();
    var month_b = $('.month_b').val();
    if ((month_a == '' || !isIntNum(month_a)) && crrStatus == 1) {
        sweetAlert('', '起始月不能为空或请输入数字', 'error');
        return;
    }
    if ((month_b == '' || !isIntNum(month_a)) && crrStatus == 1) {
        sweetAlert('', '结束月不能为空或请输入数字', 'error');
        return;
    }
    if (parseInt(month_a) > parseInt(month_b) && crrStatus == 1) {
        sweetAlert('', '起始月不能大于结束月', 'error');
        return;
    }
    if (month_a <= 0 && crrStatus == 1) {
        sweetAlert('', '起始月不能小于零', 'error');
        return;
    }
    var rate_a = $('.rate_a').val();
    var rate_b = $('.rate_b').val();
    if (rate_a != '' && !isIntNum(rate_a)) {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (rate_b != '' && !isIntNum(rate_b)) {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(rate_a) > parseInt(rate_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    var tag_a = $('.tag_a').val();
    var tag_b = $('.tag_b').val();
    if (tag_a != '' && !isIntNum(tag_a)) {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (tag_b != '' && !isIntNum(tag_b)) {
        sweetAlert('', '请输入数字', 'error');
        return;
    }
    if (parseInt(tag_a) > parseInt(tag_b)) {
        sweetAlert('', '起始数值不能大于结束数值', 'error');
        return;
    }
    if (crrStatus == 0) {
        var select_time = $('.select_time').val();
        if (select_time == 0) {
            var months = [1, 12];
        } else {
            var months = [select_time,select_time];
        }
    } else if (crrStatus == 1) {
        var months = [month_a, month_b];
    }

    var rates = [rate_a == '' ? 0 : rate_a, rate_b == '' ? 100 :rate_b];
    var tags = [tag_a == '' ? 111 : tag_a, tag_b == '' ? 111 : tag_b];
    var cityId = $('.city').val();
    var gwStatus = $('.gw_status').val();
    var provinceId = $('.province').val();
    var data = {
        province: provinceId,
        city: cityId,
        gwStatus: gwStatus,
        times: months,
        rates: rates,
        tags: tags,
        msg: msg
    };
    data.usernum = usernum;
    data.status = status;
    $.ajax({
        type: 'POST',
        url: '/admin/GW/gwPersonnelList',
        dataType: 'JSON',
        data: data,
        success: function (res) {
            if (res.error == 1 || res.msg.length <= 0) {
                addNotify();
                return;
            }
            var data = res.msg;
            setItem('orders', data);
            var totalPage = Math.ceil(data.length / 15);
            showPage(1, totalPage, addInfo);recovery();
            addInfo(1, totalPage);
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function (error) {
            console.log(error);
        }
    });
}

function addInfo(page, totalPage) {
    var data = pageData(page);
    if (data.length <= 0) {
        addNotify();
        return;
    }
    showPage(page, totalPage, addInfo);
    $('.table_hover tr').remove();
    $('.tr_item_title th').remove();
    $('.tr_checkbox th').remove();
    if (msg == 'success') {
        var orderCount = '过单数';
        var orderRatio = '过单比';
        var orderRate = '过单率';
    } else if (msg == 'overdue') {
        var orderCount = '逾期数';
        var orderRatio = '逾期比';
        var orderRate = '逾期率';
    }
    var tr = ''; var th = '';
    tr += "<th>序号</th> <th>当前状态</th> <th>入职时间</th>";
    tr += "<th>GW姓名</th> <th>GW岗位号</th><th>城市</th> ";
    tr += "<th>CS岗位号</th> <th>省份</th> <th>SZ岗位号</th>";
    tr += "<th>总"+orderCount+"</th> <th>总"+orderRatio+"</th> <th>总"+orderRate+"</th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    th += " <th><input type='checkbox' class='checkbox_input'></th><th><input type='checkbox' class='checkbox_input'></th>";
    var trStatus = true;
    for (var i = 0; i < data.length; i++) {
        var status = '';
        var tag = false;
        if (data[i].status == 1) status = '禁用';
        else status = '正常';
        var str = '';
        str += "<td>" + (i + 1) + "</td>";
        str += "<td>" + status + "</td>";
        str += "<td>" + data[i].time + "</td>";
        str += "<td>" + data[i].gwname + "</td>";
        str += "<td>" + data[i].gwusernum + "</td>";
        str += "<td>" + data[i].city + "</td>";
        str += "<td>" + data[i].csusernum + "</td>";
        str += "<td>" + data[i].province + "</td>";
        str += "<td>" + data[i].szusernum + "</td>";
        var orders = data[i].orders;
        for (var j = 0; j < orders.length; j++) {
            if (j == 0) {
                str += "<td>" + orders[j].orderTotalCount + "</td>";
                str += "<td>" + orders[j].orderTotalRatio + "</td>";
                str += "<td>" + orders[j].orderTotalRate.toFixed(2) + "%</td>";
            } else {
                str += "<td>" + orders[j].orderCount + "</td>";
                str += "<td>" + orders[j].orderRatio + "</td>";
                str += "<td>" + orders[j].orderRate.toFixed(2) + "%</td>";
            }
            if (orders[j].tag) {
                tag = true;
            }
        }
        var times = data[i].times;
        if (trStatus) {
            for (var j = parseInt(times[0]); j <= parseInt(times[1]); j++) {
                var month = '';
                if (j == 1) {
                    month = '当月';
                } else if (j == 2) {
                    month = '上月';
                } else {
                    month = '第'+j+'月';
                }
                tr += "<th>" + month + orderCount+"</th>";
                tr += "<th>" + month + orderRatio+"</th>";
                tr += "<th>" + month + orderRate+"</th>";
                th += "<th><input type='checkbox' class='checkbox_input'></th>";
                th += "<th><input type='checkbox' class='checkbox_input'></th>";
                th += "<th><input type='checkbox' class='checkbox_input'></th>";
            }
        }
        trStatus = false;
        var style = tag ? "color:red" : "";
        str += "<td class='action_list'  value='"+data[i].gwusernum+"' style='color: red'><span>查看详情</span></td>";
        var str2 = "<tr style="+style+">" + str + "</tr>";
        $('.table_hover').append(str2);
    }
    tr += "<th class='action'>操作</th>";
    th += "<th calss=\"btn_rate btn_filter\" id=\"btn_filter\"><button type='button' class='btn btn-warning btn_filter' >过滤</button></th>";
    th += "<th calss=\"btn_recovery\" id=\"btn_recovery\"><button type='button' class='btn btn-warning btn_recovery' >重置</button></th>";
    $('.tr_item_title').append(tr);
    $('.tr_checkbox').append(th);
    if (!filterTag) {
        tableHide();
    } else {
        recovery();
        // selectChecked();
    }
}

$('body').on('click', '.action_list',function(){
    var usernum = $(this).attr('value');
    window.location.href = window.location.origin+'/admin/Stock/gw_list_s?'+usernum;
});

$('.button_export').click(function(){
    var status = $(this).val();
    var name = $(this).attr('name');
    $.ajax({
        type: 'POST',
        url: 'exportToExcelGwPersonnel',
        dataType: 'JSON',
        data: {status: status, msg: name, filterIndexs: filterIndexs},
        success: function(response){
            location.href = response.path;
        },
        beforeSend: function() {
            $('.modal_Load').show();
        },
        complete: function() {
            $('.modal_Load').hide();
        },
        error: function(data){
            console.log(data);
        }
    });
});

// $('.button_export_curr').click(function(){
//     return ExcellentExport.excel(this, 'datatable', '当前报表');
// });